<%@ page contentType="text/html;charset=UTF-8" %>
<%
    String path = request.getContextPath();
    request.setCharacterEncoding("UTF-8");
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <base href="<%=basePath%>">
    <title>海达保单对账</title>
    <jsp:include page="/WEB-INF/jsp/common/common.jsp"/>
    <link href="<%=path%>/css/calendar.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="<%=path%>/css/zTreeStyle/zTreeStyle.css">
    <link rel="stylesheet" href="<%=path%>/css/base.css">
    <script type="text/javascript" src="<%=path%>/js/jquery.ztree.core-3.5.js"></script>
    <script type="text/javascript" src="<%=path%>/js/jquery.ztree.excheck-3.5.js"></script>
    <script src="<%=path%>/js/jquery.divbox.js" type="text/javascript"></script>
    <script type="text/javascript">
        // 用于存储查询条件
        var haidaMap = {};

        $(document).ready(function () {
            isReadyIframe();
            defaultDate();
        });

        function isReadyIframe() {
            var iframe = $(window.parent.document).find("#iframe").length;
            if (iframe === 0) {
                window.location.href = "<%=basePath%>index.jsp";
            }
        }

        // 获取表单数据 返回JSON对象
        function getFormParam(formId) {
            var form = $("#" + formId);
            var data = decodeURIComponent(form.serialize());
            return JSON.parse(formToJson(data));
        }

        // 将form序列化转换为json数据
        function formToJson(data) {
            // 清除所有空格
            data = data.replace(/\s/g, "");
            data = data.replace(/&/g, "\",\"");
            data = data.replace(/=/g, "\":\"");
            data = "{\"" + data + "\"}";
            return data;
        }

        // 默认为最近一个月的时间
        function defaultDate() {
            var begin = new Date();
            var end = new Date();
            begin.setMonth(new Date().getMonth() - 1);
            var beginTime = begin.format("yyyy-MM-dd");
            var endTime = end.format("yyyy-MM-dd");
            $("#startTransDate").val(beginTime);
            $("#endTransDate").val(endTime);
        }

        // 点击查询按钮
        function queryInfoByForm(pageIndex) {
            $.openLoading();
            var jsonObj = getFormParam("searchForm");
            if (!jsonObj) {
                return false;
            }
            if (!pageIndex) {
                pageIndex = 1;
            }
            jsonObj["pageIndex"] = pageIndex;
            var jsonData = JSON.stringify(jsonObj);
            $.ajax({
                url: '<%=path%>/haida/queryReconciliationInfo',
                type: 'POST',
                async: false,
                contentType: "application/json;charset=UTF-8",
                dataType: 'json',
                data: jsonData,
                success: function (page) {
                    $.closeLoading();
                    if ($.validate(page)) return;
                    var haidaPolicyTable = $("#haidaPolicyTable");
                    var policyPageDiv = $("#policyPageDiv");
                    if (page.result === 'error') {
                        // 清空表格数据
                        haidaPolicyTable.find("tbody").remove();
                        policyPageDiv.html("");
                        $.message("error", "查询数据为空，请正确填写查询条件")
                        return;
                    }
                    if (page.data.length === 0) {
                        //清空表格数据
                        haidaPolicyTable.find("tbody").remove();
                        policyPageDiv.html("");
                        $.message("error", "查询数据为空，请正确填写查询条件");
                        return;
                    }
                    haidaMap = page.map;
                    var toCancelButton = "<input class='input_button_1' name='toCancel' type='button' onclick='terminationOfInsurance($(this))' value='退保'/>";
                    var fieldName = "id,serialNumber,orderNo,polNo,prem,amt,transDate,effDate,termDate,bbrName,accountFlag"
                    var tableContent = creatTable1(fieldName, page, null, toCancelButton);
                    var tableObj = haidaPolicyTable;
                    tableObj.find("tbody").remove();
                    tableObj.append(tableContent);

                    var strPage = creatChannelPage(pageIndex, 6, page.maxPage, "104")
                    policyPageDiv.html("");
                    policyPageDiv.append(strPage);
                    // 查询后隐藏第一列ID
                    hiddenFirst();
                },
                error: function () {
                    $.closeLoading();
                    $.message("error", "系统错误，请联系管理员");
                }
            })
        }

        // 导出到Excel按钮
        function exportExcel() {
            var queryCondition = "";
            // 查询条件，false：查询条件全部为空，true查询条件不为空
            var flag = false;
            var num = 0;
            if (haidaMap !== {}) {
                $.map(haidaMap, function (value, key) {
                    //遍历map判断上一次的查询条件是否为空，不为空的拼接为参数
                    if (value !== null && value !== '') {
                        if (num === 0) {
                            queryCondition += "?" + key + "=" + value;
                        } else {
                            queryCondition += "&" + key + "=" + value;
                        }
                        flag = true;
                        num++;
                    }
                })
            }
            if (flag === false) {
                $.dialog({
                    type: "warn",
                    content: "查询条件为空，是否全量导出？全量查询将耗费大量时间！<br>请输入查询条件点击查询后再进行导出。",
                    onOk: function () {
                        exportExcelDownload(queryCondition);
                    }
                })
            } else {
                exportExcelDownload(queryCondition);
            }
        }

        // 导出为Excel表格的url
        function exportExcelDownload(queryConditionParam) {
            window.open('<%=basePath%>' + "haida/exportExcel" + queryConditionParam);
        }

        // 退保按钮
        function terminationOfInsurance(t) {
            $.dialog({
                type: "warn",
                content: "确认退保？确认后当前保单将被终止！",
                onOk: function () {
                    // 退保
                    terminationInsurance(t);
                }
            })
        }

        // 退保
        function terminationInsurance(t) {
            $.openLoading();
            // 从当前按钮查找当前数据的保单号
            var serialNumber = t.parent().parent().children().eq(1).text();
            var orderNo = t.parent().parent().children().eq(2).text();
            var polNo = t.parent().parent().children().eq(3).text();
            console.log(polNo)
            $.ajax({
                url: '<%=path%>/haida/terminationOfInsurance',
                type: 'POST',
                async: false,
                dataType: 'JSON',
                data: {
                    serialNumber: serialNumber,
                    orderNo: orderNo,
                    polNo: polNo
                },
                success: function (data) {
                    $.closeLoading();
                    if ($.validate(data)) return;
                    queryInfoByForm();
                }
            })
        }

        // 隐藏第一列ID列
        function hiddenFirst() {
            var theadID = $("#haidaPolicyTable thead tr th:nth-child(1)");
            var tbodyID = $("#haidaPolicyTable tbody tr td:nth-child(1)");
            theadID.hide();
            tbodyID.hide();
        }

        // 日期格式化
        Date.prototype.format = function (format) {
            var o = {
                "M+": this.getMonth() + 1, //month
                "d+": this.getDate(),    //day
                "h+": this.getHours(),   //hour
                "m+": this.getMinutes(), //minute
                "s+": this.getSeconds(), //second
                "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
                "S": this.getMilliseconds() //millisecond
            }
            if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
                (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o) if (new RegExp("(" + k + ")").test(format))
                format = format.replace(RegExp.$1,
                    RegExp.$1.length === 1 ? o[k] :
                        ("00" + o[k]).substr(("" + o[k]).length));
            return format;
        }
    </script>
</head>
<body>
<div class="current">
    <table class="current_table_1">
        <tbody>
        <tr class="f2">
            <td><img src="<%=path%>/images/current_1.jpg" alt=""></td>
            <td class="current_table_1_td">
                <img src="<%=path%>/images/current_1.png" alt="">&nbsp;您现在的位置：&nbsp;
                <span style="color:#136bab;">海达信息
                        <span style="padding:0 5px; font-weight:bold; line-height:28px;">&gt;</span>海达保单对账
                </span>
            </td>
            <td><img src="<%=path%>/images/current_3.jpg" alt=""></td>
        </tr>
        </tbody>
    </table>
</div>
<table border="1" class="table_02" id="table" style="height: 150px">
    <tr>
        <td>
            <%-- form用于重置按钮和获取表单序列化，新增表单元素记得name属性要加 --%>
            <form method="post" id="searchForm" name="searchForm" onsubmit="return false" action="">
                <table class="search_table2" style="margin: 12px auto;">
                    <tbody>
                    <tr>
                        <td>交易流水号：</td>
                        <td>
                            <label><input id="serialNumber" name="serialNumber" class="input_text" type="text"/></label>
                        </td>
                        <td>订单号：</td>
                        <td>
                            <label><input id="orderNo" name="orderNo" type="text" class="input_text"/></label>
                        </td>
                        <td>保单号：</td>
                        <td>
                            <label><input id="polNo" name="polNo" type="text" class="input_text"/></label>
                        </td>
                    </tr>
                    <tr>
                        <td>交易日期：</td>
                        <td>
                            <label><input id="startTransDate" name="startTransDate"
                                          onclick="showCalendar('startTransDate', 'y-mm-dd')"
                                          readonly="readonly" class="input_time"/></label>
                        </td>
                        <td>至</td>
                        <td>
                            <label><input id="endTransDate" name="endTransDate"
                                          onclick="showCalendar('endTransDate', 'y-mm-dd')"
                                          readonly="readonly" class="input_time"/></label>
                        </td>
                        <td>被保险人：</td>
                        <td>
                            <label><input id="bbrName" name="bbrName" type="text" class="input_text"/></label>
                        </td>
                    </tr>
                    <tr>
                        <td>保险起期：</td>
                        <td>
                            <label><input id="effDate" name="effDate" onclick="showCalendar('effDate', 'y-mm-dd')"
                                          readonly="readonly" class="input_time"/></label>
                        </td>
                        <td>保险止期：</td>
                        <td>
                            <label><input id="termDate" name="termDate" onclick="showCalendar('termDate', 'y-mm-dd')"
                                          readonly="readonly" class="input_time"/></label>
                        </td>
                        <td>对账标识：</td>
                        <td>
                            <label><select id="accountFlag" name="accountFlag">
                                <option value="">--请选择--</option>
                                <option value="未对账">未对账</option>
                                <option value="对账成功">对账成功</option>
                                <option value="对账失败">对账失败</option>
                            </select></label>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="6" style="padding-top: 10px; text-align: center">
                            <input id="queryInfo" type="button" class="input_button_1" value="查询"
                                   onclick="queryInfoByForm()"/>
                            <input id="reset" type="reset" class="input_button_1" value="重置"/>
                        </td>
                    </tr>
                </table>
            </form>
        </td>
    </tr>
    <tr>
        <td>
            <table class="search_table" style="margin: auto;">
                <tr>
                    <td>
                        <input id="exportExcel" type="button" class="input_button_4" value="导出Excel"
                               onclick="exportExcel()"/>
                    </td>

                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td valign="top">
            <div id="policyHeaderDiv" style="width:98%;margin:5px">
                <table class="table_02" id="haidaPolicyTable">
                    <thead>
                    <tr>
                        <th style="width:10%;display: none" nowrap="nowrap">ID</th>
                        <th style="width:10%" nowrap="nowrap">交易流水号</th>
                        <th style="width:10%" nowrap="nowrap">订单号</th>
                        <th style="width:15%" nowrap="nowrap">保单号</th>
                        <th style="width:5%" nowrap="nowrap">保费</th>
                        <th style="width:5%" nowrap="nowrap">保额</th>
                        <th style="width:10%" nowrap="nowrap">交易日期</th>
                        <th style="width:10%" nowrap="nowrap">保险起期</th>
                        <th style="width:10%" nowrap="nowrap">保险止期</th>
                        <th style="width:7%" nowrap="nowrap">被保险人</th>
                        <th style="width:7%" nowrap="nowrap">对账标识</th>
                        <th style="width:7%" nowrap="nowrap">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
                <div id="policyPageDiv" class="page"></div>
            </div>
        </td>
    </tr>
</table>
</body>
</html>
